package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    // 添加购物车
    @Override
    @Transactional  // 开启事务  多个语句
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //判断商品是否存在  update nummber or insert this dish/setmeal
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //注入当前用户id  ThreadLocal
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("当前user id：{}", BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list != null && list.size() > 0){
            //在这里只有可能有一条或者没有
            ShoppingCart cartService = list.get(0);
            //将数量加1
            cartService.setNumber(cartService.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cartService);
        }
        else{

            //判断当前插入的是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if(dishId != null){
                //当前是菜品

                Dish dish = dishMapper.queryById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else{
                Long setmealId = shoppingCart.getSetmealId();
                //当前是套餐

                Setmeal setmeal = setmealMapper.queryById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //插入数据
            shoppingCart.setNumber(1);
            //设置创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //插入
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    // 查看购物车
    @Override
    public List<ShoppingCart> showShoppingCart() {
        log.info("查看购物车");
        ShoppingCart shoppingCart =ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    // 清空购物车
    @Override
    public void cleanShoppingCart() {
        log.info("清空购物车");
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        shoppingCartMapper.delete(shoppingCart);
    }




    // 删除购物车的一个商品
    @Override
    public void removeShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车一个商品");
        //判断商品是否有两个以上
        //应该先查询购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询到该条商品
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        //对该件商品的情况应该数据只有一条
        if(shoppingCarts.size()>0 && shoppingCarts != null) {

            ShoppingCart cart = shoppingCarts.get(0);
            if (cart.getNumber() > 1) {
                //执行更新
                log.info("当前这商品不止一件,执行更新");
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(cart);
            } else {
                //执行删除
                log.info("当前这商品只有一件,执行删除");
                shoppingCartMapper.delete(shoppingCart);
            }
        }
    }







}
